package cn.cqows.task;


import cn.cqows.core.SerialPortCore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/*
 *
 * @author  FHY
 * @date  2018/5/28 11:33
 *
 */
//@Configuration
//@Component
public class SerialPortTask implements CommandLineRunner {

    @Value("${serialPortName}")
    private String serialPortName;

    @Autowired
    private SerialPortCore serialPortCore;

    @Override
    public void run(String... args) throws Exception {
        // 连接
        serialPortCore.connect(serialPortName);

        Thread.sleep(1000);
        // 发送消息
        serialPortCore.sendMsg(zhuanhuan("55 02 33 FF FF FF FF 86"));
        serialPortCore.sendMsg(zhuanhuan("55 01 33 FF FF FF FF 85"));
        serialPortCore.sendMsg(zhuanhuan("55 03 33 FF FF FF FF 87"));
    }

    public byte [] zhuanhuan(String msgStr){
        String[] msgStrSZ = msgStr.split(" ");
        byte[] bytes = new byte[msgStrSZ.length];
        for (int i = 0; i < msgStrSZ.length; i++) {
            bytes[i] = (byte) Integer.parseInt(msgStrSZ[i],16);
        }
        return bytes;
    }
}
